package com.airbat.prize.modules.base.controller.structure;

import com.airbat.prize.common.pojo.CommonResult;
import com.airbat.prize.common.pojo.PageResult;
import com.airbat.prize.excel.core.util.ExcelUtils;
import com.airbat.prize.modules.base.controller.structure.vo.*;
import com.airbat.prize.modules.base.convert.structure.BaseStructureConvert;
import com.airbat.prize.modules.base.dal.dataobject.structure.BaseStructureDO;
import com.airbat.prize.modules.base.service.structure.BaseStructureService;
import com.airbat.prize.operatelog.core.annotations.OperateLog;
import com.airbat.prize.security.core.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.airbat.prize.common.pojo.CommonResult.success;
import static com.airbat.prize.operatelog.core.enums.OperateTypeEnum.*;
import static com.airbat.prize.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.airbat.prize.security.core.util.SecurityFrameworkUtils.getLoginUser;

@Api(tags = "组织架构")
@RestController
@RequestMapping("/base/structure")
@Validated
public class BaseStructureController {

    @Resource
    private BaseStructureService structureService;

    @PostMapping("/create")
    @ApiOperation("创建组织架构")
    @PreAuthorize("@ss.hasPermission('base:structure:create')")
    public CommonResult<String> createStructure(@Valid @RequestBody BaseStructureCreateReqVO createReqVO) {
        return success(structureService.createStructure(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新组织架构")
    @PreAuthorize("@ss.hasPermission('base:structure:update')")
    public CommonResult<Boolean> updateStructure(@Valid @RequestBody BaseStructureUpdateReqVO updateReqVO) {
        structureService.updateStructure(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除组织架构")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:structure:delete')")
    public CommonResult<Boolean> deleteStructure(@RequestParam("id") String id) {
        structureService.deleteStructure(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得组织架构")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('base:structure:query')")
    public CommonResult<BaseStructureRespVO> getStructure(@RequestParam("id") String id) {
        BaseStructureDO structure = structureService.getStructure(id);
        return success(BaseStructureConvert.INSTANCE.convert(structure));
    }

    @GetMapping("/list")
    @ApiOperation("获得组织架构列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('base:structure:query')")
    public CommonResult<List<BaseStructureRespVO>> getStructureList(@RequestParam("ids") Collection<String> ids) {
        List<BaseStructureDO> list = structureService.getStructureList(ids);
        return success(BaseStructureConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-cache")
    @ApiOperation("从缓存中获得组织架构列表")
    @PreAuthorize("@ss.hasPermission('base:structure:query')")
    public CommonResult<List<BaseStructureRespVO>> getStructureCacheList() {
        LoginUser loginUser = getLoginUser();
        String structureId = loginUser.getStructureId();
        //获取递归机构数据
        List<BaseStructureDO> baseStructureCaches = new ArrayList<>();
        if (!StringUtils.isEmpty(structureId)) {
            baseStructureCaches.add(structureService.getStructure(structureId));
            baseStructureCaches.addAll(structureService.getStructuresByParentIdFromCache(structureId, true));
        }
        return success(BaseStructureConvert.INSTANCE.convertList(baseStructureCaches));
    }

    @GetMapping("/page")
    @ApiOperation("获得组织架构分页")
    @PreAuthorize("@ss.hasPermission('base:structure:query')")
    public CommonResult<PageResult<BaseStructureRespVO>> getStructurePage(@Valid BaseStructurePageReqVO pageVO) {
        PageResult<BaseStructureDO> pageResult = structureService.getStructurePage(pageVO);
        return success(BaseStructureConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出组织架构 Excel")
    @PreAuthorize("@ss.hasPermission('base:structure:export')")
    @OperateLog(type = EXPORT)
    public void exportStructureExcel(@Valid BaseStructureExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<BaseStructureDO> list = structureService.getStructureList(exportReqVO);
        // 导出 Excel
        List<BaseStructureExcelVO> datas = BaseStructureConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "组织架构.xls", "数据", BaseStructureExcelVO.class, datas);
    }

}
